//
//  vechileDetailViewController.m
//  WHB_cargo
//
//  Created by 徐国庆 on 15/12/11.
//  Copyright (c) 2015年 turing. All rights reserved.
//

#import "orderDetailViewController.h"
#import "CWStarRateView.h"

@interface orderDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)OrderModel  *order;     // 订单

@end

@implementation orderDetailViewController

- (instancetype)initWithOrder:(OrderModel *)order{
    self = [super init];
    if (self) {
        self.order = order;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navtitle.text = @"货单详情";
    [self initWithView];
}

#pragma mark UITableDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *firstCell = @"firstCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:firstCell];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:firstCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (indexPath.row == 0) {
            
            float firstHeight = 30;
            // "订单状态"
            UIView *stateView = [self titleAndContent:@"订单状态:" andContent:self.order.orderStatus withToAddress:nil];
            stateView.frame = CGRectMake(0, 0, kUIScreenWidth, firstHeight);
            [cell.contentView addSubview:stateView];
            
            // “车牌号”
            UIView *numberView = [self titleAndContent:@"取货状态:" andContent:@"2015年11月11日" withToAddress:nil];
            numberView.frame = CGRectMake(0, stateView.frame.origin.y + stateView.frame.size.height, kUIScreenWidth, firstHeight);
            [cell.contentView addSubview:numberView];
            
            // "地址"
            UIView *addressView = [self titleAndContent:@"位置:" andContent:self.order.startAddressName withToAddress:self.order.endAddressName];
            addressView.frame = CGRectMake(0, numberView.frame.origin.y + numberView.frame.size.height, kUIScreenWidth, firstHeight * 2);
            [cell.contentView addSubview:addressView];
            
            // “介质”
            UIView *mediumView = [self titleAndContent:@"介质:" andContent:self.order.mediumName withToAddress:nil];
            mediumView.frame = CGRectMake(0, addressView.frame.origin.y + addressView.frame.size.height, kUIScreenWidth, firstHeight);
            [cell.contentView addSubview:mediumView];
            
            // “重量”
            UIView *weightView = [self titleAndContent:@"重量:" andContent:self.order.weight withToAddress:nil];
            weightView.frame = CGRectMake(0, mediumView.frame.origin.y + mediumView.frame.size.height, kUIScreenWidth, firstHeight);
            [cell.contentView addSubview:weightView];
            
            // ”价格区间“
            UIView *priceView = [self titleAndContent:@"价格区间:" andContent:self.order.expectFreight withToAddress:nil];
            priceView.frame = CGRectMake(0, weightView.frame.origin.y + weightView.frame.size.height, kUIScreenWidth, firstHeight);
            [cell.contentView addSubview:priceView];
            
            // "价格区间"下面的 “线”
            UILabel *priceViewBottomLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, priceView.frame.origin.y + priceView.frame.size.height + 5, kUIScreenWidth - 20, 0.5)];
            priceViewBottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"d0d0d0"];
            [cell.contentView addSubview:priceViewBottomLineLabel];
            
            // ”备注“
            UIView *remarkView = [self titleAndContent:@"备注:" andContent:self.order.note withToAddress:nil];
            remarkView.frame = CGRectMake(0, priceView.frame.origin.y + priceView.frame.size.height, kUIScreenWidth, firstHeight);
            [cell.contentView addSubview:remarkView];
            return cell;
        } else if (indexPath.row == 1){
            UIView *view = [self twoAndStreeView:@"取货方:" sendAndReceive:self.order.getOrgName isIdentification:self.order.getOrgAuthStatus starSevel:0.8 phone:@"15146001603"];
            view.frame = CGRectMake(0, 0, kUIScreenWidth, 80);
            [cell.contentView addSubview:view];
            return cell;
        } else if (indexPath.row == 2){
            UIView *view = [self twoAndStreeView:@"收货方:" sendAndReceive:self.order.receiptOrgName isIdentification:self.order.receiptOrgAuthStatus starSevel:0.6 phone:@"15146001603"];
            view.frame = CGRectMake(0, 0, kUIScreenWidth, 80);
            [cell.contentView addSubview:view];
            
            return cell;
        } else if (indexPath.row == 3){
            UIView *view = [self twoAndStreeView:@"承运方:" sendAndReceive:self.order.carrierOrgName isIdentification:self.order.carrierOrgAuthStatus starSevel:0.4 phone:@"15146001603"];
            view.frame = CGRectMake(0, 0, kUIScreenWidth, 80);
            [cell.contentView addSubview:view];
            
            return cell;
        } else if (indexPath.row == 4){
            float firstHeight = 30;
            // "货单号:"
            UIView *numberView = [self titleAndContent:@"货单号:" andContent:self.order.orderNo withToAddress:nil];
            numberView.frame = CGRectMake(0, 0, kUIScreenWidth, firstHeight);
            [cell.contentView addSubview:numberView];
            
            // "下单时间:"
            UIView *orderTimeView = [self titleAndContent:@"下单时间:" andContent:self.order.orderTime withToAddress:nil];
            orderTimeView.frame = CGRectMake(0, numberView.frame.origin.y + numberView.frame.size.height, kUIScreenWidth, firstHeight);
            [cell.contentView addSubview:orderTimeView];
            
            // "取货确认:"
            UIView *receiveTureView = [self titleAndContent:@"取货确认:" andContent:self.order.takeTime withToAddress:nil];
            receiveTureView.frame = CGRectMake(0, orderTimeView.frame.origin.y + orderTimeView.frame.size.height, kUIScreenWidth, firstHeight);
            [cell.contentView addSubview:receiveTureView];
            
            // "送达确认:"
            UIView *sendTureView = [self titleAndContent:@"送达确认:" andContent:self.order.deliveryTime withToAddress:nil];
            sendTureView.frame = CGRectMake(0, receiveTureView.frame.origin.y + receiveTureView.frame.size.height, kUIScreenWidth, firstHeight);
            [cell.contentView addSubview:sendTureView];
            
            // "签收时间:"
            UIView *signTureView = [self titleAndContent:@"签收时间:" andContent:self.order.signTime withToAddress:nil];
            signTureView.frame = CGRectMake(0, sendTureView.frame.origin.y + sendTureView.frame.size.height, kUIScreenWidth, firstHeight);
            [cell.contentView addSubview:signTureView];
            
            return cell;
        }else if (indexPath.row == 5){
            float firstHeight = 30;
            // 车主评星
            UIView *vehicleStarView = [self fiveCellView:@"车主评星:" withStar:[self.order.driverEvaluationStar floatValue]];
            vehicleStarView.frame = CGRectMake(0, 0, kUIScreenWidth, firstHeight);
            [cell.contentView addSubview:vehicleStarView];
            
            // 货主星评
            UIView *cargoView = [self fiveCellView:@"货主评星:" withStar:[self.order.ownerEvaluationStar floatValue]];
            cargoView.frame = CGRectMake(0, vehicleStarView.frame.origin.y + vehicleStarView.frame.size.height, kUIScreenWidth, firstHeight);
            [cell.contentView addSubview:cargoView];
            return cell;
        }else{
            return cell;
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"车辆详情");
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 250;
    } else if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3){
        return 70;
    } else if (indexPath.row == 4){
        return 160;
    }else if (indexPath.row == 5){
        return 80;
    }
    return 220;
}
/**
 * 拨打电话按钮点击事件
 *
 *  @param button 电话号码
 */
- (void)phoneButtonAction:(UIButton *)button{
    NSLog(@"拨打电话");
    NSString *phoneNum = @"15146001603";
    
    UIWebView *callphonewebview;
    if (callphonewebview==nil) {
        callphonewebview=[[UIWebView alloc]init];
    }
    [self.view addSubview:callphonewebview];
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneNum]]];
    [callphonewebview loadRequest:request];
}

/**
 *  初始化视图
 */
- (void)initWithView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight - 40) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
}

/**
 *  自定义cell，
 *
 *  @param title     标题
 *  @param content   内容
 *  @param toAddress 如果是地址显示到达的位置 如果该参数为nil自动设置为不显示
 *
 *  @return 生成一个标题和一个内容的view
 */
- (UIView *)titleAndContent:(NSString *)title andContent:(NSString *)content withToAddress:(NSString *)toAddress{
    UIView *bottomView = [[UIView alloc] init];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 20)];
    titleLabel.text = title;
    titleLabel.textAlignment = 2;
    titleLabel.font = [UIFont systemFontOfSize:15];
    [bottomView addSubview:titleLabel];
    
    // 内容
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x + titleLabel.frame.size.width + 20, 10, 200, 20)];
    contentLabel.text = content;
    contentLabel.textAlignment = 0;
    [contentLabel sizeToFit];
    contentLabel.font = [UIFont systemFontOfSize:15];
    
    // 如果content是已签收，设置详情的字体颜色
    if ([content isEqualToString:@"已签收"]) {
        contentLabel.textColor = [UIColor HexStrWithColor:@"CE0000"];
    } else if ([content isEqualToString:@"已确认"]){
        contentLabel.textColor = [UIColor HexStrWithColor:@"CE0000"];
    }
//     if ([contentLabel.text isEqualToString:@"已确认"]) {
//        contentLabel.textColor = [UIColor HexStrWithColor:@"8CEA00"];
//    } else if ([contentLabel.text isEqualToString:@"未确认"]){
//        contentLabel.textColor = [UIColor HexStrWithColor:@"FF2D2D"];
//    }
    [bottomView addSubview:contentLabel];
    
    if ([title isEqualToString:@"位置:"]) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(contentLabel.frame.origin.x + contentLabel.frame.size.width / 2, contentLabel.frame.origin.y + contentLabel.frame.size.height + 5, 18 / 2.0, 21 / 2.0)];
        imageView.image = [UIImage imageNamed:@"jt_xl"];
        imageView.transform=CGAffineTransformMakeRotation(1.55);
        [bottomView addSubview:imageView];
        
        UILabel *toAddLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentLabel.frame.origin.x, imageView.frame.origin.y + imageView.frame.size.height + 5, 200, 20)];
        toAddLabel.text = toAddress;
        [bottomView addSubview:toAddLabel];
    }
    
    return bottomView;
}

/**
 *  自定义cell
 *
 *  @param title            标题
 *  @param sendAndReceive   送货方和取货方
 *  @param isIdentification 是否被认证
 *  @param starSevel        星级等级
 *  @param phoneNumber      电话号
 *
 *  @return 生成一个标题，取货当送货方，星级，电话视图
 */
- (UIView *)twoAndStreeView:(NSString *)title sendAndReceive:(NSString *)sendAndReceive isIdentification:(BOOL)isIdentification starSevel:(float)starSevel phone:(NSString *)phoneNumber{
    UIView *bottomView = [[UIView alloc] init];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 20)];
    titleLabel.text = title;
    titleLabel.textAlignment = 2;
    titleLabel.font = [UIFont systemFontOfSize:15];
    [bottomView addSubview:titleLabel];
    
    // 内容
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x + titleLabel.frame.size.width + 20, 10, 200, 20)];
    contentLabel.text = sendAndReceive;
    contentLabel.textAlignment = 0;
    [contentLabel sizeToFit];
    [bottomView addSubview:contentLabel];
    contentLabel.font = [UIFont systemFontOfSize:15];
    contentLabel.textColor = [UIColor HexStrWithColor:@"CE0000"];
    // 是否认证
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(contentLabel.frame.origin.x + contentLabel.frame.size.width + 5, contentLabel.frame.origin.y + 5, 25, 15)];
    if (isIdentification) {
        imageView.image = [UIImage imageNamed:@"yrz"];
    } else{
        imageView.image = [UIImage imageNamed:@"wrz"];
    }
    [bottomView addSubview:imageView];
    
    // 服务评价
    UILabel *evaluateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, titleLabel.frame.origin.y + titleLabel.frame.size.height + 10, 80, 20)];
    evaluateLabel.text = @"服务评价:";
    evaluateLabel.textAlignment = 2;
    [bottomView addSubview:evaluateLabel];
    
    // 星
    CWStarRateView *ratingControl = [[CWStarRateView alloc]initWithFrame:CGRectMake(evaluateLabel.frame.origin.x + evaluateLabel.frame.size.width + 20, evaluateLabel.frame.origin.y, 110, 20)];
    [bottomView addSubview:ratingControl];
    ratingControl.scorePercent = starSevel;
    ratingControl.hasAnimation = YES;
    
    // 拨打电话
    UIButton *phoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    phoneButton.frame = CGRectMake(kUIScreenWidth - 70, (50 - 30) / 2, 60, 30);
//    [phoneButton setImage:[UIImage imageNamed:@"yrz"] forState:UIControlStateNormal];
    [phoneButton setTitle:@"拨打电话" forState:UIControlStateNormal];
    [phoneButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    phoneButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [phoneButton addTarget:self action:@selector(phoneButtonAction:) forControlEvents:UIControlEventTouchDown];
    [bottomView addSubview:phoneButton];
    
    return bottomView;
}

/**
 *  自定义cell
 *
 *  @param title     标题
 *  @param starSevel 星级等级
 *
 *  @return 返回整个视图
 */
- (UIView *)fiveCellView:(NSString *)title withStar:(float)starSevel{
    
    UIView *bottomView = [[UIView alloc] init];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 20)];
    titleLabel.text = title;
    titleLabel.textAlignment = 2;
    [bottomView addSubview:titleLabel];
    titleLabel.font = [UIFont systemFontOfSize:15];
    
    // 星
    CWStarRateView *ratingControl = [[CWStarRateView alloc]initWithFrame:CGRectMake(titleLabel.frame.origin.x + titleLabel.frame.size.width + 20, titleLabel.frame.origin.y, 110, 20)];
    [bottomView addSubview:ratingControl];
    ratingControl.scorePercent = starSevel;
    ratingControl.hasAnimation = YES;
    
    return bottomView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
